<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>渐隐渐现轮播图</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        ul,
        ol,
        li {
            list-style: none;
        }

        .banner {
            width: 600px;
            height: 400px;
            border: 10px solid #333;
            margin: 100px auto;
            position: relative;
        }

        .banner>ul {
            width: 100%;
            height: 100%;
            position: relative;
        }

        .banner>ul>li {
            height: 100%;
            width: 100%;
            line-height: 400px;
            text-align: center;
            font-size: 100px;
            color: #fff;
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
            transition: opacity .5s linear;
        }

        .banner>ul>li.active {
            opacity: 1;
        }

        .banner>ol {
            width: 175px;
            padding-right: 15px;
            position: absolute;
            left: 50%;
            height: 25px;
            bottom: 30px;
            margin-left: -93px;
            background-color: rgba(0, 0, 0, .5);
            border-radius: 15px;
            padding-top: 5px;
        }

        .banner>ol>li {
            width: 20px;
            height: 20px;
            background-color: #fff;
            border-radius: 50%;
            float: left;
            margin-left: 15px;
            cursor: pointer;
        }

        .banner>ol>li.active {
            background-color: purple;
        }

        .banner>div {
            width: 100%;
            height: 60px;
            position: absolute;
            left: 0;
            top: 50%;
            margin-top: -30px;
        }

        .banner>div>p {
            float: left;
            width: 40px;
            height: 100%;
            background-color: rgba(0, 0, 0, .5);
            line-height: 60px;
            text-align: center;
            font-size: 30px;
            color: #fff;
            cursor: pointer;
        }

        .banner>div>.right {
            float: right;
        }
    </style>
</head>

<body>
    <div class="banner">
        <ul class="img_box">
            <li class="active" style="background-color: red;">1</li>
            <li style="background-color: skyblue;">2</li>
            <li style="background-color: violet;">3</li>
            <li style="background-color: black;">4</li>
            <li style="background-color: palegreen;">5</li>
        </ul>
        <ol>
            <!-- <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li> -->
        </ol>
        <div>
            <p class="left">
                << /p>
                    <p class="right">></p>
        </div>
    </div>

    <script>
        /**
         * 
        */
        const imgBox = document.querySelector('.banner > ul')
        const pointBox = document.querySelector('.banner > ol')
        const banner = document.querySelector('.banner')

        let index = 0
        let timer = 0

        setPoint()
        function setPoint() {
            var num = imgBox.children.length
            const frg = document.createDocumentFragment()
            for (let i = 0; i < num; i++) {
                const li = document.createElement('li')
                li.classList.add('point')
                li.dataset.point = i
                if (i === 0) li.classList.add('active')
                frg.appendChild(li)
            }
            pointBox.appendChild(frg)
            pointBox.style.width = num * 35 + 'px'
            pointBox.style.marginLeft = -pointBox.offsetWidth / 2 + 'px'
        }

        function changeOne(type) {
            imgBox.children[index].classList.remove('active')
            pointBox.children[index].classList.remove('active')
            if (type === true) {
                index++
            } else if (type === false) {
                index--
            } else {
                index = type
            }
            if (index >= imgBox.children.length) {
                index = 0
            }
            if (index < 0) {
                index = imgBox.children.length - 1
            }
            imgBox.children[index].classList.add('active')
            pointBox.children[index].classList.add('active')
        }

        autoPlay()
        function autoPlay() {
            timer = setInterval(() => {
                changeOne(true)
            }, 1000)
        }

        overOut()
        function overOut() {
            banner.addEventListener('mouseover', () => clearInterval(timer))
            banner.addEventListener('mouseout', () => autoPlay())
        }

        bindEvent()
        function bindEvent() {
            banner.addEventListener('click', e => {
                e = e || window.event
                const target = e.target || e.srcElement
                if (target.className === 'right') {
                    changeOne(true)
                }
                if (target.className === 'left') {
                    changeOne(false)
                }
                if (target.className === 'point') {
                    const pointIndex = target.dataset.point - 0
                    changeOne(pointIndex)
                }
            })
        }

        changeTab()
        function changeTab() {
            document.addEventListener('visibilitychange', () => {
                if (document.visibilityState === 'hidden') {
                    clearInterval(timer)
                } else if (document.visibilityState === 'visible') {
                    autoPlay()
                }
            })
        }

    </script>

</body>

</html>